import pymysql as db
import cgitb; cgitb.enable()
import re

conn = db.connect (host = "localhost",
            user = "student",
            passwd = "anything",
            db = "emanuelacademy")
cursor = conn.cursor()

env = os.environ.get
formData = cgi.FieldStorage()

validHTML = ['<b>', '<i>', '<u>']

def changeUserAlias(newAlias):
    cursor.execute("UPDATE users SET userAlias = '%s' WHERE username = '%s'" %(newAlias, user))
    conn.commit()
    cursor.execute("SELECT * FROM users WHERE username = '%s'" %user)
    row = cursor.fetchone()
    return row[2]


def isValidAlias(newAlias):
    no_html = re.compile(r'[^<>]+')
    italics = re.compile('<\s*i\s*>[^<>]<\s*/\s*i\s*>')
    underline = re.compile('<\s*u\s*>[^<>]<\s*/\s*u\s*>')
    
    return no_html.match(newAlias) or italics.match(newAlias) or underline.match(newAlias)

assert isValidAlias('Goldy')
assert isValidAlias('saul_herman1994')
assert isValidAlias('<u>Goldy</u>')
assert isValidAlias('<i>saul_herman1994</i>')
assert not isValidAlias('<u>Goldy</i>')
assert not isValidAlias('<i>Goldy</u>')
assert not isValidAlias('<b>Goldy</b>')
assert not isValidAlias('<b>Goldy</b>')
assert not isValidAlias('<?php>my_sql_query("DROP TABLE users;")</?php>')
